package cn.bsuc.crm.controller;


import cn.bsuc.crm.entity.SaleChance;
import cn.bsuc.crm.service.SaleChanceService;
import cn.bsuc.crm.util.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhuoyuan
 * @since 2022-10-03
 */
@RestController
@CrossOrigin
@RequestMapping("/chance")
public class SaleChanceController {

    @Autowired
    private SaleChanceService saleChanceService;

    @GetMapping("/selectAllchance")
    public Result selectAllchance(@RequestParam(defaultValue = "1") Integer pageNo,
                                  @RequestParam(defaultValue = "3")Integer pageSize) {
        Page<SaleChance> saleChancePage = saleChanceService.selectAllchance(pageNo,pageSize);
        return Result.ok(saleChancePage);
    }
    @GetMapping("/selectChanceById")
    public Result selectChanceById(Integer id) {
        QueryWrapper<SaleChance> saleChanceQueryWrapper = new QueryWrapper<>();
        saleChanceQueryWrapper.eq("assign_man",id);
        List<SaleChance> list = saleChanceService.list(saleChanceQueryWrapper);
        return Result.ok(list);
    }

    @GetMapping("/selectChance")
    public Result selectChance(Integer id) {
        SaleChance byId = saleChanceService.getById(id);
        return Result.ok(byId);
    }

    @PostMapping("/saveChance")
    public Result saveChance(@RequestBody  SaleChance saleChance) {
        int i = saleChanceService.saveChance(saleChance);
        return Result.ok();
    }

    @GetMapping("/deleteChance")
    public Result deleteChance(Integer[] id) {
        QueryWrapper<SaleChance> saleChanceQueryWrapper = new QueryWrapper<>();
        saleChanceQueryWrapper.eq("id",id);
        List<Integer> integers = Arrays.asList(id);
        saleChanceService.removeBatchByIds(integers);
        return Result.ok();
    }

    @PostMapping("/updatdSuccess")
    public Result updatdSuccess(@RequestBody  SaleChance saleChance) {
        saleChance.setDevResult(1);
        saleChanceService.updateById(saleChance);
        return Result.ok();
    }

    @PostMapping("/updatdFail")
    public Result updatdFail(@RequestBody  SaleChance saleChance) {
        saleChance.setDevResult(3);
        saleChanceService.updateById(saleChance);
        return Result.ok();
    }
}

